package cn.net.autocode.core.utils;

import com.alibaba.fastjson2.*;

import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;

public class JSONUtils {
	
	/**
	 * json array检索
     */
	public static JSONObject arraySearch(JSONArray source, String key, Object value) {
		int len = source.size();
		JSONObject target = null;
		for(int i=0;i<len;i++) {
			JSONObject json = source.getJSONObject(i);
			Object obj = json.get(key);
			if(obj == null) {
				continue;
			}
			if(value==obj||value.equals(obj)||value.toString().equals(obj.toString())) {
				target=json;
				break;
			}
		}
		return target;
	}
	
	
	/**
	 * jsonarray检索
     */
	public static Object valueSearch(JSONArray source,String sourceKey,Object value,String targetKey) {
		int len = source.size();
		Object target = null;
		for(int i=0;i<len;i++) {
			JSONObject json = source.getJSONObject(i);
			Object obj = json.get(sourceKey);
			if(value.equals(obj)||value.toString().equals(obj.toString())) {
				target=json.get(targetKey);
				break;
			}
		}
		return target;
	}

	/**
	 * collection转JSONArray
     */
	public static JSONArray copyOf(Collection collection) {
		JSONArray array = JSONArray.copyOf(collection);
		String jsonStr = JSON.toJSONString(array, JSONWriter.Feature.WriteMapNullValue);
		JSONArray jsonArray = JSONArray.parseArray(jsonStr, JSONReader.Feature.EmptyStringAsNull);
		return jsonArray;
	}


	/**
	 * json复制
     */
	public static JSONObject copy(JSONObject source) {
		JSONObject target = JSON.parseObject(source.toJSONString(JSONWriter.Feature.WriteMapNullValue));
		return target;
	}
	
	public static void date2Str(JSONArray array) {
		DateTimeFormatter dtf1 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
		DateTimeFormatter dtf2 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSSSSS");

		int l = array.size();
		for(int i=0;i<l;i++) {
			JSONObject json = array.getJSONObject(i);
			Set<String> keys = json.keySet();
			for(String key:keys) {
				if(json.get(key)==null){

				}else if(json.get(key) instanceof LocalDateTime) {
					LocalDateTime value = json.getObject(key,LocalDateTime.class);
					json.put(key, dtf1.format(value));
				}else if(json.get(key) instanceof Timestamp) {
					LocalDateTime value = json.getObject(key,Timestamp.class).toLocalDateTime();
					json.put(key, dtf2.format(value));
				}
			}
		}
	}
	
	public static boolean isBlank(JSONObject source,String key) {
		if(!source.containsKey(key)) {
			return true;
		}
		
		Object obj = source.get(key);
		if(obj==null) {
			return true;
		}
		
		if(obj instanceof JSONArray) {
			JSONArray tmp = (JSONArray)obj;
            return tmp.isEmpty();
		}else if(obj instanceof String) {
			if(obj.toString().trim().equals("")) {
				return true;
			}
		}
		
		return false;
	}

	public static boolean compare(JSONObject json1,JSONObject json2){
		Set<String> keys = json1.keySet();
		for(String key:keys){
			String value1 = json1.getString(key);
			String value2 = json2.getString(key);
			if(value1!=null){
				value1 = value1.trim();
			}

			if(value2!=null){
				value2 = value2.trim();
			}

			if(value1==null||value1.equals("")){
				if(value2!=null&&!value2.equals("")){
					return false;
				}
			}else{
				if(value2==null||value2.equals("")){
					return false;
				}else if(!value1.equalsIgnoreCase(value2)){
					return false;
				}
			}
		}
		return true;
	}

}
